package porridgenpickle.client;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.RootPanel;

public class Presenter {
	
	private String videoHtml = "";
	
	RootPanel root;
	Presenter(RootPanel root) {
		this.root = root;
	}
					
	void showVideoPage() {
		final VideoWidget view = new VideoWidget();
		
		final VideoServiceAsync rpcService = GWT.create(VideoService.class);
			
		Timer timer = new Timer() {
			
			@Override
			public void run() {
				rpcService.getHtml(new AsyncCallback<String>() {
					
					@Override
					public void onSuccess(String result) {
						
						if (! result.equals(videoHtml)) {
							videoHtml = result;
							view.videoIframeHtml.setHTML(videoHtml);
						}
					}
					
					@Override
					public void onFailure(Throwable caught) {
						Window.alert("RPC error. Please try again by refreshing this page");			
					}
				});						
			}
		};
		
		view.videoIframeHtml.setHTML("fetching video...");
		timer.run();
		
		final int REFRESH_TIME_MS = 30 * 1000; 
		timer.scheduleRepeating(REFRESH_TIME_MS);
				
		root.clear();
		root.add(view);		
	}
}
